package com.yqRecite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqRecite.entity.Admin;
import com.yqRecite.service.UserService;
import com.yqRecite.entity.User;
import com.yqRecite.mapper.UserMapper;
import com.yqRecite.util.result.Result;
import com.yqRecite.util.result.ResultUtil;
import com.yqRecite.util.util.GenerateYqId;
import com.yqRecite.vo.RegisterUser;
import com.yqRecite.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 增加一个用户
     * @param user
     */
    public Result addOneUser(User user){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", user.getUsername());
        User p = userMapper.selectOne(wrapper);
        if (p != null){
            return ResultUtil.fail("此账号已存在，添加失败", user);
        }
        int res = userMapper.insert(user);
        if (res <= 0){
            return ResultUtil.error("增加信息时出现未知异常", user);
        }
        return ResultUtil.ok().setDesc("增加成功").setData(user);
    }

    @Override
    public List<User> getAllUserList() {
        return userMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public int updateUser(User person) {
        return userMapper.updateById(person);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteById(id);
    }

    /**
     * 注册
     * @param registerUser
     * @return
     */
    @Override
    public Result register(RegisterUser registerUser) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", registerUser.getUsername());
        User isExistUser = userMapper.selectOne(wrapper);//判断用户名是否已经存在
        if (isExistUser != null){
            return ResultUtil.error("已经存在此用户名");
        }else {
            GenerateYqId generateYqId = new GenerateYqId();
            User user = new User();
            user.setYqId(generateYqId.GenerateYqId());//生成宇颀id
            user.setRole(registerUser.getRole());
            user.setAvatar(registerUser.getAvatar());
            user.setUsername(registerUser.getUsername());
            user.setPassword(registerUser.getPassword());
            user.setGender(registerUser.getGender());
            user.setBannerList("");
            user.setEnable(1);
            user.setCreateTime(new Date());
            int res = userMapper.insert(user);
            if (res == 1){
                return ResultUtil.ok(user);
            }else {
                return ResultUtil.fail("注册出现异常，请重试", user);
            }
        }
    }

    /**
     * 员工登录
     * @param username
     * @param password
     * @return
     */
    public Result login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
//        User res = userMapper.searchUserByNameAndPwd(username, password);
        User res = userMapper.selectOne(wrapper);
        if (res == null){
            return ResultUtil.fail("登录失败，请检查用户名和密码");
        }
        if (res.getEnable() == 0){
            return ResultUtil.fail("改账号已被封禁，请联系管理员解封后登录！");
        }
        UserVo userVo = new UserVo();
        userVo.setId(res.getId());
        userVo.setYqId(res.getYqId());
        userVo.setRole(res.getRole());
        userVo.setUsername(res.getUsername());
        userVo.setCreateTime(res.getCreateTime());
        userVo.setPassword(res.getPassword());
        userVo.setAvatar(res.getAvatar());
        userVo.setGender(res.getGender());
        userVo.setEmail(res.getEmail());
        userVo.setEnable(res.getEnable());
        userVo.setPhone(res.getPhone());
        if (res.getBannerList() != null){
            userVo.setBannerList(res.getBannerList().split(","));
        }
        userVo.setBgImgUrlId(res.getBgImgUrlId());
        userVo.setSignature(res.getSignature());
        System.out.println(userVo);
        return ResultUtil.ok().setDesc("登录成功").setData(userVo);
    }

    /**
     * 删除员工
     * @param id
     */
    public Result deleteUserById(int id){
        Integer res = userMapper.deleteById(id);
        if (res <= 0 || res == null){
            return ResultUtil.error("删除信息时出现未知异常");
        }
        return ResultUtil.ok().setDesc("删除成功");
    }

    /**
     * 修改员工信息
     * @param user
     */
    public Result updateUserById(User user){
        Integer res = userMapper.updateById(user);
        if (res <= 0 || res == null){
            return ResultUtil.error("修改信息时出现未知异常", user);
        }
        return ResultUtil.ok().setDesc("修改成功");
    }
}
